#!/usr/bin/perl -w  # for syntax highlighting
package clreqd_modules;

use strict;
use lvm;

sub drbd8_grow {
my %ARGS=@_;

	local $SIG{CHLD}='DEFAULT';
	my $vgname=$ARGS{VGNAME};
	my $lvname=$ARGS{LVNAME};
	my $drbdsetup=$ARGS{DRBDSETUP};
	my $devnum=$ARGS{DEVNUM};
	my $appname=$ARGS{APPNAME};

	my $cmd="$drbdsetup /dev/drbd$devnum resize";
	system($cmd);
	my $r=$?>>8;
	if($r) {
		return "ERROR RC=$r from 'drbdsetup /dev/drbd$devnum resize' command.";
	}

	# We now must wait for the sync to complete!
	eval "use storage_spec::drbd8;";
	my $STORAGE=eval "new storage_spec::drbd8;";
	if(!defined($STORAGE)) {
		return "ERROR Unable to use storage spec - storage_spec::drbd8\nError: " . eval "storage_spec::drbd8::get_error_msg;";
	}
	
	#########################################################
	# At this point we fork out the thing as a background	#
	# task which the lha_buildapp will probe periodically.	#
	#########################################################

	my $pid=fork();
	if($pid) {
		$::PIDS{$pid}="";
		return "OK $pid";
	}
	
	#########################################################
	# This is now running in the background. 		#
	# The "QUERY_PID PID nn" will return "RUNNING" or	#
	# "COMPLETE resultstring".				#
	#########################################################

	my $syncing=1;
	while($syncing) {
		my ($rc,@info2)=$STORAGE->sync_status($vgname,$appname);
		if(!$rc && @info2) {
			return "ERROR $info2[0]";
		}
		$syncing=0;
		for my $cinfo (@info2) {
			my ($lvname,$status,$rate)=($cinfo->[0],$cinfo->[1],$cinfo->[2]);
			if($status==2) {
				$syncing=1;
				last;
			}
		}
		select(undef,undef,undef,2);
	}

	#########################################################
	# Write out the file to a mailbox for the parent to	#
	# scan when opportunity allows.				#
	#########################################################

	my $wfd;
	my $dir="$::ENV{TRUECL_CODE}/monitors/mailbox";
	open($wfd,">$dir/.clreqd.$$") or exit(0);
	print $wfd "OK\n";
	close($wfd);
	exit(0);
}

1;
